package com.asa.mvvm_aop_frame.basic.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.asa.mvvm_aop_frame.basic.BaseContract
import com.asa.mvvm_aop_frame.basic.viewmodel.BaseViewModel
import org.koin.android.ext.android.bind
import org.koin.android.viewmodel.ext.android.getViewModel
import kotlin.reflect.KClass

/**
 * create By：anderson
 * on 2022/5/1
 * desc:
 */
abstract class BaseFragment<V:ViewDataBinding,VM:BaseViewModel>:Fragment(),BaseContract.IWindow,BaseContract.IBar,BaseContract.IView {


    protected lateinit var binding: V
    protected lateinit var viewModel:VM
    protected var activity:AppCompatActivity?=null


    protected fun isViewModelInitialized():Boolean = ::viewModel.isInitialized


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activity = getActivity() as AppCompatActivity
        initParams()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater,getLayoutId(),null,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initDataBindingAndViewModel()
        initView(savedInstanceState)
        initData()
        initViewObservable()
    }

    private fun initDataBindingAndViewModel(){
        val clazz = javaClass.kotlin.supertypes[0].arguments[1].type!!.classifier!! as KClass<VM>
        viewModel = getViewModel(clazz)
        binding.setVariable(initVariableId(),viewModel)
        binding.lifecycleOwner = this
        lifecycle.addObserver(viewModel)

    }


    fun loadRootFragment(
        containerId: Int,
        toFragment: Fragment
    ) {
        childFragmentManager.beginTransaction()
            .add(containerId, toFragment).hide(toFragment).commitAllowingStateLoss()
    }

    fun showFragment(showFragment: Fragment) {
        showFragment(showFragment, null, null)
    }

    fun showFragment(
        showFragment: Fragment,
        enterAnimResId: Int?,
        exitAnimResId: Int?
    ) {
        val beginTransaction = childFragmentManager.beginTransaction()
        if (enterAnimResId != null && exitAnimResId != null) {
            beginTransaction.setCustomAnimations(enterAnimResId, exitAnimResId)
        }
        beginTransaction.show(showFragment).commitAllowingStateLoss()
    }

    fun hideFragment(showFragment: Fragment) {
        childFragmentManager.beginTransaction()
            .hide(showFragment).commitAllowingStateLoss()
    }

    fun removeFragment(showFragment: Fragment) {
        childFragmentManager.beginTransaction()
            .remove(showFragment).commitAllowingStateLoss()
    }

}